package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"

)

//分组表
type News struct {
	Id          int64
	Title       string
	Newstype    string
	Author      string
	Newscontent string
	Status      string
	Createtime  string
	Modifytime  string
}
//建立模型
func init() {
	orm.RegisterModel(new(News))
}


//获取新闻列表
func GetNewslist(neswTitle string,newsType string ,firstTime string,lastTime string) (news []orm.Params) {
	o := orm.NewOrm()
	sql:="SELECT Title AS Title,NewsType AS Newstype,Author AS Author,NewsContent AS Newscontent,CreateTime AS  Createtime ,ModifyTime AS Modifytime FROM news WHERE 1=1"
	sql=sql+" and Status='0'"
	if neswTitle!=""{
		sql=sql+" and  Title like'%"+neswTitle+"%'"
                }
	if newsType!=""{
		sql=sql+" and  newsType ='"+newsType+"'"
	}
	if firstTime!="" && lastTime!=""{
		sql=sql+" and  Createtime >'"+firstTime+"' and  Createtime <'"+lastTime+"' "
	}
	o.Raw(sql).Values(&news)
	fmt.Println("TestSql...................",news)
	return news


}

////获取新闻列表
//func GetNewslist(page int64, page_size int64, sort string) (news []orm.Params, count int64) {
//	o := orm.NewOrm()
//	new := new(News)
//
//	qs := o.QueryTable(new).Filter("Status", "0")
//
//	var offset int64
//	if page <= 1 {
//		offset = 0
//	} else {
//		offset = (page - 1) * page_size
//	}
//	qs.Limit(page_size, offset).OrderBy(sort).Values(&news)
//	count, _ = qs.Count()
//	fmt.Println("news:", news)
//	fmt.Println("count:", count)
//	return news, count
//}

//添加新闻
func AddNews(Title string,newsType string,Author string,Newscontent string) (int64) {
	o := orm.NewOrm()
	res,_ :=o.Raw("INSERT INTO news(Title,NewsType,Author,NewsContent) VALUES(?,?,?,?)",Title,newsType,Author,Newscontent).Exec()
	id, _ := res.LastInsertId()
	fmt.Println(id)
	return id
}
////添加新闻
//func AddNews(g *News) (int64, error) {
//	o := orm.NewOrm()
//	new := new(News)
//	new.Author = g.Author
//	new.Title = g.Title
//	new.Newstype = g.Newstype
//	new.Newscontent = g.Newscontent
//	new.Createtime = time.Now().Format("2006-01-02 15:04:05")
//	new.Status = "0"
//	new.Modifytime = "无"
//	id, err := o.Insert(new)
//	fmt.Println("new.Author:", new.Author)
//	fmt.Println("new.Title:", new.Title)
//	fmt.Println("new.Newstype:", new.Newstype)
//	fmt.Println("new.NewsContent:", new.Newscontent)
//	fmt.Println("new.Createtime:", new.Createtime)
//	fmt.Println("id:", id)
//	return id, err
//}
//新闻加入回收站
func Recycle_News_One(News_id int64) (int64, error) {
	o := orm.NewOrm()
	news := new(News)
	//更改之前必须先指定主键
	news.Id = News_id
	//要更新的内容
	news.Status = "1"
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(news, "Status")
	return num, err
}

//新闻-回收站 列表
func GetNewsRecyclelist(page int64, page_size int64, sort string) (news []orm.Params, count int64) {
	o := orm.NewOrm()
	new := new(News)
	qs := o.QueryTable(new).Filter("Status", "1")
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * page_size
	}
	qs.Limit(page_size, offset).OrderBy(sort).Values(&news)
	count, _ = qs.Count()
	fmt.Println("news:", news)
	fmt.Println("count:", count)
	return news, count
}

//从回收站还原数据
func Reduction_News_One(News_id int64) (int64, error) {
	o := orm.NewOrm()
	news := new(News)
	//更改之前必须先指定主键
	news.Id = News_id
	//要更新的内容
	news.Status = "0"
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(news, "Status")
	return num, err
}

//从回收站删除数据
func Delete_News_One(News_id int64) (int64, error) {
	o := orm.NewOrm()
	news := new(News)
	//更改之前必须先指定主键
	news.Id = News_id
	//要更新的内容
	news.Status = "2"
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(news, "Status")
	return num, err
}